package com.xcc.dataStructures.demo02_queue;

import java.util.Scanner;

/**
 * 环形队列
 *
 * @author xiaocheng
 * @date 2020/11/17 15:33
 */
public class CircleQueueDemo {

    public static void main(String[] args) {
        System.out.println("========环形队列测试========");

        CircleQueue queue = new CircleQueue(3);
        while (true) {
            System.out.println();
            System.out.println("s[show]:遍历链表");
            System.out.println("h[head]:查看头元素");
            System.out.println("a[add]:添加元素");
            System.out.println("g[get]:获取元素");
            System.out.println("e[exit]:退出");
            System.out.print("请输入方法:");
            Scanner scanner = new Scanner(System.in);
            String key = scanner.next();
            switch (key) {
                case "a":
                    try {
                        System.out.print("请输入添加的值:");
                        int num = scanner.nextInt();
                        queue.add(num);
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }
                    break;
                case "g":
                    try {
                        System.out.printf("获取的元素为: %d \n", queue.get());
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }
                    break;
                case "s":
                    queue.show();
                    break;
                case "h":
                    System.out.println(queue.head());
                    break;
                case "e":
                    return;
            }
        }
    }

}

/**
 * 定义环形队列
 */
class CircleQueue {
    //队列的大小
    private int maxSize;
    //队列的头元素的索引 初始值为0
    private int front;
    //队列的最后元素的索引 初始值为0
    private int rear;
    //队列的存储
    private int[] arr;

    /**
     * 初始构造
     */
    public CircleQueue(int maxSize) {
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }

    /**
     * 插入数据
     */
    public void add(int num) {
        if (isFull()) {
            throw new RuntimeException("队列满，不能再添加!!!!!");
        }
        arr[rear] = num;
        //当前如果达到maxSize就从0开始继续加
        rear = (rear + 1) % maxSize;
    }

    /**
     * 显示队列
     */
    public void show() {
        //遍历的数据为从
        for (int i = front; i < front + valiable(); i++) {
            System.out.printf("a[%d] = %d\n", i % maxSize, arr[i % maxSize]);
        }
    }

    /**
     * 获取有效元素的个数
     */
    public int valiable() {
        return (rear  + maxSize - front) % maxSize;
    }

    /**
     * 获取数据
     */
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列空，不能再获取数据!!!!!");
        }
        int num = arr[front];
        //当前如果达到maxSize就从0开始继续加
        front = (front + 1) % maxSize;
        return num;
    }

    /**
     * 获取头元素
     */
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列空，不能再获取数据!!!!!");
        }
        return arr[front];
    }

    /**
     * 判断队列是否满
     */
    public boolean isFull() {
        //如果队列长度为3 当前rear为2 front为0 则是满队列
        return (rear + maxSize + 1) % maxSize == front;
    }

    /**
     * 判断队列空
     */
    public boolean isEmpty() {
        return rear == front;
    }

}
